import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {GetListsData, getListsParams, ApiResponse, addTodoData, TodoData} from '../models/todolists';
import {Observable, tap} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class TodolistsService {
  constructor(
    private http: HttpClient,
  ) { }

  getTodolists(params?: getListsParams): Observable<ApiResponse<GetListsData>> {
    const httpOptions = params ? { params: params as any } : {};
    return this.http.get<ApiResponse<GetListsData>>('/todos', httpOptions)
  }

  addTodoList(addTodoData: addTodoData): Observable<ApiResponse<TodoData>>{
    return this.http.post<ApiResponse<TodoData>>('/todos', addTodoData)
  }

  deleteTodo(id: string): Observable<ApiResponse<{deletedId: string}>> {
    return this.http.delete<ApiResponse<{deletedId: string}>>(`/todos/${id}`)
  }

  updateTodo(id:string, title:string): Observable<ApiResponse<TodoData>> {
    return this.http.patch<ApiResponse<TodoData>>(`/todos/${id}`, {title})
  }

  updateSelectTodos(ids:string[], completed: Boolean): Observable<ApiResponse<{updatedCount: number, updatedIds: string[]}>> {
    return this.http.patch<ApiResponse<{updatedCount: number, updatedIds: string[]}>>('/todos/bulk/update', { ids: ids, completed: completed })
  }

  deleteTodoLists(ids: string[]): Observable<ApiResponse<{deletedCount: number, deletedIds: string[]}>> {
    return this.http.delete<ApiResponse<{deletedCount: number, deletedIds: string[]}>>('/todos/bulk/delete', {
      body: {ids: ids}
    })
  }

}
